package com.example.test.restful.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.example.test.restful.entity.User;
import com.example.test.restful.model.param.UserAdd;
import com.example.test.restful.model.result.ResultObj;
import com.example.test.restful.model.result.UserDetail;
import org.springframework.web.bind.annotation.*;

/**
 * @Package: com.example.test.restful
 * @Author: Mr.Waves
 * @Date: 2020-02-15 18:15
 * @Description: restful风格的接口
 **/
@RestController
@RequestMapping("/api")
public class UserController {
    /**
     * get one
     */
    @GetMapping("/user/{id}")
    public ResultObj<User> getDetail(@PathVariable("id") Long id) {
        // mock select
        UserDetail userDetail = new UserDetail();
        User user = new User();
        user.setId(id);
        user.setName("张三");
        user.setStatus(0);
        BeanUtil.copyProperties(user, userDetail, CopyOptions.create().ignoreNullValue().ignoreError().ignoreCase());
        return new ResultObj(200,"查询成功！",user);
    }

    /**
     * add
     */
    @PostMapping("/user")
    public ResultObj<UserDetail> add(@RequestBody UserAdd userAdd) {
        // mock
        UserDetail userDetail = new UserDetail();
        userAdd.setId(345L);
        BeanUtil.copyProperties(userAdd, userDetail, CopyOptions.create().ignoreNullValue().ignoreError().ignoreCase());
        return new ResultObj<>(200,"插入成功！", userDetail);
    }

    /**
     * update
     */
    @PutMapping("/user")
    public ResultObj<UserDetail> update(@RequestBody UserAdd userAdd) {
        // mock
        UserDetail userDetail = new UserDetail();
        BeanUtil.copyProperties(userAdd, userDetail, CopyOptions.create().ignoreNullValue().ignoreError().ignoreCase());
        return new ResultObj<>(200,"更新成功！", userDetail);
    }

    /**
     * delete
     */
    @DeleteMapping("/user/{id}")
    public ResultObj remove(@PathVariable("id") Long id) {
        // mock
        return new ResultObj(200,"删除成功！");
    }

    /**
     * page
     */
    @GetMapping("/users")
    public ResultObj getPage(String keyword) {
        // mock
        return new ResultObj(200, "查询成功!", keyword);
    }
}
